package ru.ozon
{
	import mx.rpc.soap.SOAPDecoder;
	import mx.rpc.soap.SOAPResult;
	import flash.utils.getTimer;
	import flash.xml.XMLDocument;
	import flash.xml.XMLNode;
	
	import mx.collections.IList;
	import mx.core.mx_internal;
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.rpc.soap.types.ICustomSOAPType;
	import mx.rpc.wsdl.WSDLMessagePart;
	import mx.rpc.wsdl.WSDLConstants;
	import mx.rpc.wsdl.WSDLEncoding;
	import mx.rpc.wsdl.WSDLOperation;
	import mx.rpc.xml.ContentProxy;
	import mx.rpc.xml.DecodingContext;
	import mx.rpc.xml.SchemaConstants;
	import mx.rpc.xml.SchemaDatatypes;
	import mx.rpc.xml.TypeIterator;
	import mx.rpc.xml.XMLDecoder;
	import mx.utils.ObjectProxy;
	import mx.utils.object_proxy;
	import mx.utils.StringUtil;
	import mx.utils.XMLUtil;
	
	public class NONValidatingSOAPDecoder extends SOAPDecoder
	{
		
	    private var log:ILogger;
	    private var _elementsWithId:XMLList;
	    private var _forcePartArrays:Boolean;
	    private var _headerFormat:String;
	    private var _ignoreWhitespace:Boolean = true;
	    private var _multiplePartsFormat:String;
	    private var _referencesResolved:Boolean; // Used to prevent repeat resolution passes for a single document.
	    private var _resultFormat:String;
	    private var _wsdlOperation:mx.rpc.wsdl.WSDLOperation;
	

		public function NONValidatingSOAPDecoder()
		{
			super();
		}
		/**
     * Decodes a SOAP response into a result and headers. 
     */
	    //-----------------------------------------------------------------------------------------------
	    public override function decodeResponse(response:*):SOAPResult
	    {
	        var soapResult:SOAPResult;
	        var responseString:String;
	
	        if (response is XML)
	            responseString = XML(response).toXMLString(); 
	        else
	            responseString = String(response);
	
	        var startTime:int = getTimer();
	
	        // Reset the decoder state to clear contexts for schema types, etc.
	        reset();
	
	        if (responseString != null)
	        {
	
	            // Keep track of the previous ignoreWhitespace setting (as it is
	            // unfortunately a static API on the intrinsic XML type) so we
	            // can set it back to its original state once we're finished.
	            var oldIgnoreWhitespace:Boolean = XML.ignoreWhitespace;
	            try
	            {
	                // Work around Flash Player bug 192355 by removing whitespace
	                // between processing instructions and the root tag before
	                // constructing an XML instance of the SOAP response.
	                responseString = responseString.replace(PI_WHITESPACE_PATTERN, "?><");
	                responseString = StringUtil.trim(responseString);
	                XML.ignoreWhitespace = ignoreWhitespace;
	                var responseXML:XML = new XML(responseString);
	
	                soapResult = decodeEnvelope(responseXML);
	            }
	            finally
	            {
	                XML.ignoreWhitespace = oldIgnoreWhitespace;
	            }
	        }
	
	        return soapResult;
	    }
	    //-----------------------------------------------------------------------------------------------
	    protected override function decodeEnvelope(responseXML:XML):SOAPResult
	    {
	        var soapResult:SOAPResult = new SOAPResult();
	
	        var envNS:Namespace;
	
	            // Set the namespaces and the Schema uri for the decoder.
	            var schemaConst:SchemaConstants;
	            var nsArray:Array = responseXML.inScopeNamespaces();
	            for each (var ns:Namespace in nsArray)
	            {
	                schemaManager.namespaces[ns.prefix] = ns;
	            }
	
	            // SOAP Headers
	            var headerXML:XML = responseXML[soapConstants.headerQName][0];
	            if (headerXML != null)
	            {
	                soapResult.headers = decodeHeaders(headerXML);
	            }
	            // SOAP Body
	            var bodyXML:XML = responseXML[soapConstants.bodyQName][0];
	            soapResult.result = bodyXML;
	        return soapResult;
	    }
	}
}